//
//  YKFarmDetailAddController.m
//  YK_BaseMediator
//
//  Created by edward on 2021/3/12.
//  Copyright © 2021 Edward. All rights reserved.
//

#import "YKFarmDetailAddController.h"
#import <Masonry/Masonry.h>
#import <YK_ExectionModel/YKNSObjectCore.h>
#import <YK_ExectionModel/YKUIKitCore.h>

@interface YKFarmDetailAddController ()
///
@property(nonatomic,copy) void(^timeBlock)(NSString *time);
///
@property(nonatomic,strong) UIDatePicker *datePicker;
///
@property(nonatomic,strong) NSDate *getDate;
@end

@implementation YKFarmDetailAddController

- (instancetype)initWithBlock:(void(^)(NSString *time))block
{
    self = [super init];
    if (self) {
        self.title = @"选择日期";
        self.timeBlock = block;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIView *topView = UIView.instance.yk_borderColor(UIColor.blackColor);
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    UILabel *titleLabel = UILabel.instance;
    titleLabel.text = self.title;
    titleLabel.font = [UIFont systemFontOfSize:20];
    [topView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(topView.mas_centerY);
        make.centerX.mas_equalTo(topView.mas_centerX);
    }];
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
    [topView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(44);
    }];
    UIButton *comfirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [comfirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [comfirmButton setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
    [topView addSubview:comfirmButton];
    [comfirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(44);
    }];
    [self.view addSubview:self.datePicker];
    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(topView.mas_bottom).offset(10);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(300);
    }];
    
    @weakify(self);
    [[cancelButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [[comfirmButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        if (self.timeBlock) {
            NSString *date;
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            [fmt setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
            fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
            date = [fmt stringFromDate:self.getDate];
            NSString *str = [NSString stringWithFormat:@"%@",date];
            self.timeBlock(str);
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }];
}

- (UIDatePicker *)datePicker
{
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc] init];
        if (@available(iOS 13.4, *)) {
            _datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
        }
        _datePicker.backgroundColor = UIColor.whiteColor;
        _datePicker.datePickerMode = UIDatePickerModeDate;
        _datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-Hans"];
        [_datePicker addTarget:self action:@selector(timePicker:) forControlEvents:UIControlEventValueChanged];
    }
    return _datePicker;
}

- (NSDate *)getDate
{
    if (!_getDate) {
        _getDate = [NSDate date];
    }
    return _getDate;
}

- (void)timePicker:(UIDatePicker *)sender
{
    self.getDate = sender.date;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
